/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
 * the OAI Public License, Version 1.1  (the "License"); you may not use this
 * file except in compliance with the License. You may obtain a copy of the
 * License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */
/**
 * Nudm_UECM
 * Nudm Context Management Service. � 2020, 3GPP Organizational Partners (ARIB,
 * ATIS, CCSA, ETSI, TSDSI, TTA, TTC). All rights reserved.
 *
 * The version of the OpenAPI document: 1.1.2
 *
 *
 * NOTE: This class is auto generated by OpenAPI Generator
 * (https://openapi-generator.tech). https://openapi-generator.tech Do not edit
 * the class manually.
 */

#include "SMFSmfRegistrationApiImpl.h"

#include "udm_client.hpp"
#include "logger.hpp"
#include "udm_config.hpp"
#include "udm.h"

extern oai::udm::config::udm_config udm_cfg;

namespace oai {
namespace udm {
namespace api {

using namespace oai::udm::model;
using namespace oai::udm::app;
using namespace oai::udm::config;

SMFSmfRegistrationApiImpl::SMFSmfRegistrationApiImpl(
    std::shared_ptr<Pistache::Rest::Router> rtr, udm_app* udm_app_inst,
    std::string address)
    : SMFSmfRegistrationApi(rtr) {}

void SMFSmfRegistrationApiImpl::get_smf_registration(
    const std::string& ueId, const Pistache::Optional<Snssai>& singleNssai,
    const Pistache::Optional<std::string>& dnn,
    const Pistache::Optional<std::string>& supportedFeatures,
    Pistache::Http::ResponseWriter& response) {
  response.send(
      Pistache::Http::Code::Ok,
      "SMFSmfRegistrationApiImpl::get_smf_registration API has not been "
      "implemented yet!\n");
}
void SMFSmfRegistrationApiImpl::registration(
    const std::string& ueId, const int32_t& pduSessionId,
    const SmfRegistration& smfRegistration,
    Pistache::Http::ResponseWriter& response) {
  std::string udr_ip =
      std::string(inet_ntoa(*((struct in_addr*) &udm_cfg.udr_addr.ipv4_addr)));
  std::string udr_port = std::to_string(udm_cfg.udr_addr.port);
  std::string remoteUri;
  std::string Method;
  std::string msgBody;
  std::string Response;
  nlohmann::json j_ProblemDetails;
  ProblemDetails m_ProblemDetails;

  // TODO: to move it to UDM_APP
  // UDR GET interface
  // get SmfRegistration related info
  remoteUri = udr_ip + ":" + udr_port + NUDR_DATA_REPOSITORY +
              udm_cfg.udr_addr.api_version + "/subscription-data/" + ueId +
              "/context-data/smf-registrations/" + std::to_string(pduSessionId);
  Logger::udm_uecm().debug("PUT Request:" + remoteUri);
  Method = "PUT";

  nlohmann::json smfRegistration_j;
  to_json(smfRegistration_j, smfRegistration);
  long http_code;
  http_code = udm_client::curl_http_client(
      remoteUri, Method, Response, smfRegistration_j.dump());

  nlohmann::json response_data = {};
  try {
    Logger::udm_uecm().debug("PUT Response:" + Response);
    response_data = nlohmann::json::parse(Response.c_str());

  } catch (nlohmann::json::exception& e) {  // error handling
    Logger::udm_uecm().info("Could not get JSON content from UDR response");

    m_ProblemDetails.setCause("USER_NOT_FOUND");
    m_ProblemDetails.setStatus(404);
    m_ProblemDetails.setDetail("User " + ueId + " not found in Database");
    to_json(j_ProblemDetails, m_ProblemDetails);

    Logger::udm_uecm().error("User " + ueId + " not found in Database");
    Logger::udm_uecm().info("Send 404 Not_Found response to client");

    response.headers().add<Pistache::Http::Header::ContentType>(
        Pistache::Http::Mime::MediaType("application/problem+json"));

    response.send(
        Pistache::Http::Code::Not_Found, j_ProblemDetails.dump().c_str());
    return;
  }
  Logger::udm_uecm().debug("HTTP Response code %d", http_code);

  // Set content type

  response.headers().add<Pistache::Http::Header::ContentType>(
      Pistache::Http::Mime::MediaType("application/json"));

  response.send(
      static_cast<Pistache::Http::Code>(http_code),
      smfRegistration_j.dump().c_str());
}

}  // namespace api
}  // namespace udm
}  // namespace oai
